package com.example.cushion.realize.frame.main;

import android.bluetooth.BluetoothDevice;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.cushion.R;
import com.example.cushion.base.BaseFragment;

import org.itzheng.and.baseutils.common.DelayedUtils;
import org.itzheng.and.baseutils.log.LogHelper;
import org.itzheng.and.baseutils.ui.UIUtils;
import org.itzheng.and.ble.bean.BluetoothDeviceInfo;
import org.itzheng.and.ble.callback.BleScanCallback;
import org.itzheng.and.ble.utils.BleScanUtils;

import java.util.List;

/**
 * Created by root on 17-5-7.
 */

public class MainFragment extends BaseFragment {
    private View rootView;

    public static MainFragment newFragment() {
        return new MainFragment();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if (rootView == null) {
            rootView = View.inflate(getActivity(), R.layout.fragment_main, null);
//            TextView textView = new TextView(getActivity());
//            textView.setTextColor(Color.RED);
//            textView.setText("Main Fragment");
//            textView.setPadding(100, 200, 0, 0);
//            rootView = textView;
        }
        return rootView;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        testBle();
    }

    private static final String TAG = "MainFragment";

//    private void testBle() {
//        //全局唯一ble操作工具
////        BleOptionUtils bleOptionUtils = App.getInstance().getBleOptionUtils();
//        final BleScanUtils bleScanUtils = BleScanUtils.newInstance();
//        bleScanUtils.startLeScan(new BleScanCallback() {
//            @Override
//            public void onLeScan(List<BluetoothDeviceInfo> list) {
//
//            }
//
//            @Override
//            public void onLeScan(BluetoothDevice bluetoothDevice, int i, byte[] bytes) {
//                LogHelper.d(TAG, bluetoothDevice.getName() + "::" + bluetoothDevice.getAddress());
//            }
//        });
//    }

    private boolean isExit = false;
    DelayedUtils handler = DelayedUtils.newInstance();
    private Runnable exitRunnable = new Runnable() {
        @Override
        public void run() {
            isExit = false;
        }
    };

    @Override
    public boolean onBackPressed() {
        if (isExit) {
            handler.removeCallbacks(exitRunnable);
            getActivity().moveTaskToBack(true);
            return true;
        } else {
            isExit = true;
            UIUtils.showToast(R.string.main_hint_on_back_pressed);
            DelayedUtils.newInstance().postDelayed(exitRunnable, 2000);
        }
        if (super.onBackPressed()) {

        }
        return true;
    }
}
